package com.rj.bd.Login.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.log.Log;
import com.rj.bd.Login.entity.Login;
import com.rj.bd.Login.service.ILoginService;
import com.rj.bd.utils.CodeUtils;
import com.rj.bd.utils.EmailUtil;
import com.rj.bd.utils.JiaMi;
import com.rj.bd.utils.NanoIdUtils;
import com.rj.bd.utils.SmsCodeUtil;

import sun.print.resources.serviceui;


/**
 * @DECS C层
 * @author 20565
 *
 */
@Controller
@RequestMapping(value="/login")
public class LoginController {
	@Autowired
	public ILoginService loginService;
	
	
	
	@ResponseBody
	@RequestMapping("/login")
	public Map<String, Object> queryLogin(@RequestBody Login login){
		Map<String, Object> map=new HashMap<String, Object>();
		
		System.out.println("LoginController:queryLogin()");
		
		Login login2=loginService.queryById(login);
		
		System.out.println(login2);
		if (login2==null) {
			map.put("code", "-1");
			map.put("msg", "该用户不存在");
		}
		else if (!(login2.getPassword().equals(login.getPassword()))) {
			map.put("code", "-1");
			map.put("msg", "用户名或密码错误");
			
		}
		else{
			
			map.put("code", "200");
			map.put("msg", "登录成功");
			map.put("token",NanoIdUtils.randomNanoId(login2.getLoginId()+login2.getPassword()));
		}
		return map;
	
	}
	
	
		@ResponseBody
		@RequestMapping("/querypage")
		public Map<String, Object> querypage(HttpServletRequest request,@RequestBody Login login){
			Map<String, Object> map=new HashMap<String, Object>();
			String loginId=login.getLoginId();
			Login loginbyid=loginService.queryById1(loginId);	
			String loginId2=loginbyid.getLoginId();
			String lEmail = loginbyid.getLEmail();
			map.put("账号", loginId2);
			map.put("电子邮箱", lEmail);
			map.put("code", 200);
			map.put("msg", "查询成功");
			return map;
		}
	@ResponseBody
	@RequestMapping("/reception")
	public Map<String, Object> reception(HttpServletRequest request,@RequestBody Login login) throws UnsupportedEncodingException, MessagingException{
		Map<String, Object> map=new HashMap<String, Object>();
		Login reception=loginService.reception(login);
		
		if (reception.getLEmail().equals(login.getLEmail())) {
			map.put("msg","邮箱正确");
			String code = CodeUtils.createCode();
			EmailUtil.createMineMessage(reception.getLEmail(), "", "验证码","您的验证码为:  "+code+" 请记住验证码,不要泄露。");
			reception.setLcode(code);
			loginService.insert(reception);
			map.put("code", 200);
			map.put("msg", "验证码添加成功");
		}
		
		else {
			map.put("code", "-1");
			map.put("msg", "邮箱错误");
		}
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/code")
	public Map<String, Object>code(HttpServletRequest request,@RequestBody Login login){
		Map<String, Object> map=new HashMap<String, Object>();
		Login dcode=loginService.code(login);
		if (dcode.getLcode().equals(login.getLcode())) {
			dcode.setLcode("");
			loginService.delete(dcode);
			String loginId3=dcode.getLoginId();
			map.put("账号", loginId3);
			
		}
		else {
			loginService.delete(dcode);
			map.put("code", "-1");
			map.put("msg", "验证码错误");
		}
		
		return map;
	}
	@ResponseBody
	@RequestMapping(value="/accept")
	public Map<String, Object> accept(@RequestBody Login login){
		Map<String,Object> map=new HashMap<String, Object>();
		loginService.editpassword(login);
		map.put("code", 200);
		map.put("msg", "修改成功");
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/queryphone")
	public Map<String, Object> queryphone(@RequestBody Login login){
		Map<String, Object> map=new HashMap<String, Object>();
		String loginId=login.getLoginId();
		Login loginbyid=loginService.queryById1(loginId);
		String loginId2=loginbyid.getLoginId();
		String lEmail = loginbyid.getLEmail();
		String lphone = loginbyid.getLphone();
		String email=JiaMi.emailJiaMi(lEmail);
		System.out.println(email);
		String phone = JiaMi.phoneJiaMi(lphone);
		System.out.println(phone);
		map.put("账号", loginId2);
		map.put("邮箱", email);
		map.put("电话", phone);
		map.put("code", 200);
		map.put("msg", "查询成功");
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/getcode")
	public Map<String, Object> getcode(HttpServletRequest request,@RequestBody Login login) throws  ClientProtocolException, IOException{
		Map<String, Object> map=new HashMap<String, Object>();
		Login reception=loginService.reception(login);
		
		if (reception.getLphone().equals(login.getLphone())) {
			map.put("msg","电话正确");
			SmsCodeUtil.sendSmsCode(login.getLphone());
			map.put("code", 200);
			map.put("msg", "验证码添加成功");
		}
		
		else {
			map.put("code", "-1");
			map.put("msg", "电话错误");
		}
		
		return map;
	}
	
	@ResponseBody
    @RequestMapping(value = "/register")
    public Map<String,Object> register(@RequestBody Login login) throws IOException{
    	Map<String, Object> map =new HashMap<String, Object>();
    	Login dcode=loginService.code(login);
    	System.out.println(login.getLphone()+","+login.getLcode());
    	
    		map=new HashMap<String, Object>();
    		
			if(SmsCodeUtil.verifyCode(login.getLphone(),login.getLcode())){
				String zhanghuid=dcode.getLoginId();
				map.put("账号", zhanghuid);
				map.put("code", 200);
				map.put("msg", "验证成功");
				return map;
			}else{
				map.put("code", -1);
				map.put("msg", "验证码错误");
				return map;
			}
		
    }
	
	
}